package acwing._1_1AlgorithmBasic;

/**
 * @Project : ProgrammingExercises
 * @Package : ACWing.AlgorithmTemplate
 * @File : _1_3HighAccuracy.java
 * @Author : WangRuoyu
 * @Date 2023/3/10 10:41
 */

public class _1_3HighAccuracy {
    static String highAccuracyAdd(String a, String b) { // 加法运算
        int[] aa = new int[Math.max(a.length(), b.length())];
        int[] bb = new int[Math.max(a.length(), b.length())];

        for (int i = 0, j = a.length() - 1; i < a.length(); ++i, --j) {
            aa[j] = a.charAt(i) - '0';
        }

        for (int i = 0, j = b.length() - 1; i < b.length(); ++i, --j) {
            bb[j] = b.charAt(i) - '0';
        }

        StringBuilder c = new StringBuilder();

        int t = 0;
        int tmp = 0;
        for (int i = 0; i < aa.length; ++i) {
            tmp = aa[i] + bb[i] + t;
            c.append(tmp % 10);
            t = tmp / 10;
        }

        return c.reverse().toString();
    }

    static int sasb(String a, String b) {
        if (a.length() > b.length()) {
            return 1;
        } else if (b.length() > a.length()) {
            return -1;
        } else {
            for (int i = 0; i < a.length(); ++i) {
                if (a.charAt(i) > b.charAt(i)) {
                    return 1;
                } else if (b.charAt(i) > a.charAt(i)) {
                    return -1;
                }
            }
            return 0;
        }
    }

    static String highAccuracySub(String a, String b) { // 减法运算：先判断大小，大数减小数，去除高位0再加符号
        int signal = sasb(a, b);
        if (signal == 0) {
            return "0";
        }

        int[] aa = new int[Math.max(a.length(), b.length())];
        int[] bb = new int[Math.max(a.length(), b.length())];

        for (int i = 0, j = a.length() - 1; i < a.length(); ++i, --j) {
            aa[j] = a.charAt(i) - '0';
        }

        for (int i = 0, j = b.length() - 1; i < b.length(); ++i, --j) {
            bb[j] = b.charAt(i) - '0';
        }

        StringBuilder c = new StringBuilder();

        int t = 0;
        int tmp = 0;
        for (int i = 0; i < aa.length; ++i) {
            tmp = signal * (aa[i] - bb[i]) - t;
            if (tmp >= 0) {
                c.append(tmp);
                t = 0;
            } else {
                c.append(tmp + 10);
                t = 1;
            }
        }

        c.reverse();

        while (c.length() >= 0 && c.charAt(0) == '0') {
            c.deleteCharAt(0);
        }

        if (signal == -1) {
            c.insert(0, '-');
        }

        return c.toString();
    }

    static String highAccuracyMul(String a, int b) {
        if (b == 0 || "0".equals(a)) {
            return "0";
        }

        StringBuilder c = new StringBuilder(a.length());

        int t = 0;
        for (int i = a.length() - 1; i >= 0; --i) {
            t += (a.charAt(i) - '0') * b;
            c.append(t % 10);
            t /= 10;
        }
        c.append(t);

        return c.reverse().toString();
    }

    static String highAccuracyDiv(String a, int b) {
        if (b == 0) {
            return "输入错误";
        } else if ("0".equals(a)) {
            return "0";
        }

        StringBuilder c = new StringBuilder(a.length());

        int t = 0;
        for (int i = 0; i < a.length(); ++i) {
            t = (t * 10 + (a.charAt(i) - '0'));
            c.append(t / b);
            t %= b;
        }

        while (c.length() >= 0 && c.charAt(0) == '0') {
            c.deleteCharAt(0);
        }

        c.append(" ").append(t);

        return c.toString();
    }
}


